{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "import neuralnet\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn import preprocessing\n",
    "from sklearn.metrics import precision_score, confusion_matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     kong         drill    D     N  ROM    L     V rock\n",
      "0       1  (1,0,0,0,0)   110   200  150  250  3.63   砂岩\n",
      "1       1  (0,1,0,0,0)   110   200  150  250  4.50   砂岩\n",
      "2       1  (1,0,0,0,0)   110   200  150  250  3.08   砂岩\n",
      "3       1  (1,0,0,0,0)   110   200  150  250  2.38   泥岩\n",
      "4       1  (1,0,0,0,0)   110   200  150  250  0.89   砂岩\n",
      "..    ...           ...  ...   ...  ...  ...   ...  ...\n",
      "199     7  (0,0,1,0,0)    73  1000  150  250  4.53   砂岩\n",
      "200     7  (0,0,0,1,0)    89  1000  150  250  2.10   煤岩\n",
      "201     7  (0,0,1,0,0)    91  1000  150  250  3.06   泥岩\n",
      "202     7  (0,0,1,0,0)    91  1000  150  250  0.92   灰岩\n",
      "203     7  (0,0,1,0,0)    91  1000  150  250  3.32   泥岩\n",
      "\n",
      "[204 rows x 8 columns]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\env\\pytorch\\lib\\site-packages\\ipykernel_launcher.py:4: FutureWarning: \n",
      ".ix is deprecated. Please use\n",
      ".loc for label based indexing or\n",
      ".iloc for positional indexing\n",
      "\n",
      "See the documentation here:\n",
      "http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#ix-indexer-is-deprecated\n",
      "  after removing the cwd from sys.path.\n",
      "d:\\env\\pytorch\\lib\\site-packages\\ipykernel_launcher.py:6: FutureWarning: \n",
      ".ix is deprecated. Please use\n",
      ".loc for label based indexing or\n",
      ".iloc for positional indexing\n",
      "\n",
      "See the documentation here:\n",
      "http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#ix-indexer-is-deprecated\n",
      "  \n",
      "d:\\env\\pytorch\\lib\\site-packages\\ipykernel_launcher.py:7: FutureWarning: \n",
      ".ix is deprecated. Please use\n",
      ".loc for label based indexing or\n",
      ".iloc for positional indexing\n",
      "\n",
      "See the documentation here:\n",
      "http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#ix-indexer-is-deprecated\n",
      "  import sys\n",
      "d:\\env\\pytorch\\lib\\site-packages\\ipykernel_launcher.py:8: FutureWarning: \n",
      ".ix is deprecated. Please use\n",
      ".loc for label based indexing or\n",
      ".iloc for positional indexing\n",
      "\n",
      "See the documentation here:\n",
      "http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#ix-indexer-is-deprecated\n",
      "  \n",
      "d:\\env\\pytorch\\lib\\site-packages\\ipykernel_launcher.py:10: FutureWarning: \n",
      ".ix is deprecated. Please use\n",
      ".loc for label based indexing or\n",
      ".iloc for positional indexing\n",
      "\n",
      "See the documentation here:\n",
      "http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#ix-indexer-is-deprecated\n",
      "  # Remove the CWD from sys.path while we load stuff.\n",
      "d:\\env\\pytorch\\lib\\site-packages\\ipykernel_launcher.py:11: FutureWarning: \n",
      ".ix is deprecated. Please use\n",
      ".loc for label based indexing or\n",
      ".iloc for positional indexing\n",
      "\n",
      "See the documentation here:\n",
      "http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#ix-indexer-is-deprecated\n",
      "  # This is added back by InteractiveShellApp.init_path()\n"
     ]
    }
   ],
   "source": [
    "data = pd.read_csv('../data.csv')\n",
    "# 将带有中文属性的词转换为数值\n",
    "print(data)\n",
    "listUniq = data.ix[:, 'drill'].unique()\n",
    "for j in range(len(listUniq)):\n",
    "    data.ix[:, 'drill'] = data.ix[:, 'drill'].apply(\n",
    "        lambda x: j if x == listUniq[j] else x)\n",
    "listUniq = data.ix[:, 'rock'].unique()\n",
    "for j in range(len(listUniq)):\n",
    "    data.ix[:, 'rock'] = data.ix[:, 'rock'].apply(\n",
    "        lambda x: j if x == listUniq[j] else x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\env\\pytorch\\lib\\site-packages\\sklearn\\preprocessing\\_encoders.py:415: FutureWarning: The handling of integer data will change in version 0.22. Currently, the categories are determined based on the range [0, max(values)], while in the future they will be determined based on the unique values.\n",
      "If you want the future behaviour and silence this warning, you can specify \"categories='auto'\".\n",
      "In case you used a LabelEncoder before this OneHotEncoder to convert the categories to integers, then you can now use the OneHotEncoder directly.\n",
      "  warnings.warn(msg, FutureWarning)\n"
     ]
    }
   ],
   "source": [
    "# 进行one-hot编码\n",
    "tempdata = data[['drill']]\n",
    "enc = preprocessing.OneHotEncoder()\n",
    "enc.fit(tempdata)\n",
    "# 将结果转换为二维数组\n",
    "tempdata = enc.transform(tempdata).toarray()\n",
    "# 再将二维数组转换为DataFrame，记得这里会变成多列\n",
    "tempdata = pd.DataFrame(tempdata, columns=['drill'] * len(tempdata[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\env\\pytorch\\lib\\site-packages\\sklearn\\preprocessing\\_encoders.py:415: FutureWarning: The handling of integer data will change in version 0.22. Currently, the categories are determined based on the range [0, max(values)], while in the future they will be determined based on the unique values.\n",
      "If you want the future behaviour and silence this warning, you can specify \"categories='auto'\".\n",
      "In case you used a LabelEncoder before this OneHotEncoder to convert the categories to integers, then you can now use the OneHotEncoder directly.\n",
      "  warnings.warn(msg, FutureWarning)\n",
      "d:\\env\\pytorch\\lib\\site-packages\\sklearn\\preprocessing\\_encoders.py:415: FutureWarning: The handling of integer data will change in version 0.22. Currently, the categories are determined based on the range [0, max(values)], while in the future they will be determined based on the unique values.\n",
      "If you want the future behaviour and silence this warning, you can specify \"categories='auto'\".\n",
      "In case you used a LabelEncoder before this OneHotEncoder to convert the categories to integers, then you can now use the OneHotEncoder directly.\n",
      "  warnings.warn(msg, FutureWarning)\n"
     ]
    }
   ],
   "source": [
    "# 将输出转换为one-hot编码\n",
    "rockdata = data[['rock']]\n",
    "enc_rock = preprocessing.OneHotEncoder()\n",
    "enc_rock.fit(rockdata)\n",
    "rockdata = enc_rock.transform(rockdata).toarray()\n",
    "\n",
    "# 将孔洞转换为one-hot编码\n",
    "kongdata = data[['kong']]\n",
    "enc_kong = preprocessing.OneHotEncoder()\n",
    "enc_kong.fit(kongdata)\n",
    "kongdata = enc_kong.transform(kongdata).toarray()\n",
    "kongdata = pd.DataFrame(kongdata, columns=['kong'] * len(kongdata[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     drill  drill  drill  drill  drill    D     N  ROM    L\n",
      "0      1.0    0.0    0.0    0.0    0.0  110   200  150  250\n",
      "1      0.0    1.0    0.0    0.0    0.0  110   200  150  250\n",
      "2      1.0    0.0    0.0    0.0    0.0  110   200  150  250\n",
      "3      1.0    0.0    0.0    0.0    0.0  110   200  150  250\n",
      "4      1.0    0.0    0.0    0.0    0.0  110   200  150  250\n",
      "..     ...    ...    ...    ...    ...  ...   ...  ...  ...\n",
      "199    0.0    0.0    1.0    0.0    0.0   73  1000  150  250\n",
      "200    0.0    0.0    0.0    0.0    1.0   89  1000  150  250\n",
      "201    0.0    0.0    1.0    0.0    0.0   91  1000  150  250\n",
      "202    0.0    0.0    1.0    0.0    0.0   91  1000  150  250\n",
      "203    0.0    0.0    1.0    0.0    0.0   91  1000  150  250\n",
      "\n",
      "[204 rows x 9 columns]\n"
     ]
    }
   ],
   "source": [
    "# 将其他数据读取到x_columns中\n",
    "x_columns = [x for x in data.columns if x in ['D', 'N', 'L', 'V', 'ROM']]\n",
    "# x = pd.concat([kongdata, tempdata, data[x_columns]],  axis=1)\n",
    "# 将探头数据与其他数据合并作为输入\n",
    "x = pd.concat([tempdata, data[x_columns]], axis=1)\n",
    "X = np.array(x)\n",
    "# np.random.shuffle(X)\n",
    "# 归一化\n",
    "X = (X - X.min(axis=0)) / (X.max(axis=0) - X.min(axis=0))\n",
    "# X = X.transpose()\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "# Y为一个(204, )的数组，y为（204， 4）的数组\n",
    "Y = data['rock']\n",
    "y = np.array(rockdata)\n",
    "# select = np.random.randint(0, 204, size=[40,])\n",
    "select = random.sample(range(0, X.shape[0]), 40)\n",
    "select = np.sort(select)\n",
    "train_select = []\n",
    "for i in range(0, X.shape[0]):\n",
    "    if i not in select:\n",
    "        train_select.append(i)\n",
    "train_select = np.array(train_select)\n",
    "x_test = X[select, :]\n",
    "y_test = y[select, :]\n",
    "x_train = X[train_select, :]\n",
    "y_train = y[train_select, :]\n",
    "Y_train = y_train.argmax(axis=1)\n",
    "Y_test = y_test.argmax(axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Doing Gradient Checking....\n",
      "第0次数损失为2.9236836151240606\n",
      "第1000次数损失为1.8306691402245614\n",
      "第2000次数损失为1.7927520787910995\n",
      "第3000次数损失为1.7846798764421512\n",
      "第4000次数损失为1.7791555518684652\n",
      "第5000次数损失为1.7735911946843335\n",
      "第6000次数损失为1.7677021115585363\n",
      "第7000次数损失为1.761435518609078\n",
      "第8000次数损失为1.754710535139942\n",
      "第9000次数损失为1.7474054068233587\n",
      "第10000次数损失为1.7392497636783626\n",
      "第11000次数损失为1.7299309555165348\n",
      "第12000次数损失为1.7192026755551275\n",
      "第13000次数损失为1.706784661470073\n",
      "第14000次数损失为1.692117322177353\n",
      "第15000次数损失为1.6741359080883447\n",
      "第16000次数损失为1.6517161896098729\n",
      "第17000次数损失为1.625583730855921\n",
      "第18000次数损失为1.597969126135805\n",
      "第19000次数损失为1.570889516062925\n",
      "第20000次数损失为1.5469915559891851\n",
      "第21000次数损失为1.526470568064251\n",
      "第22000次数损失为1.5074477897837562\n",
      "第23000次数损失为1.4876019156934117\n",
      "第24000次数损失为1.4649241580063224\n",
      "第25000次数损失为1.4391175979666884\n",
      "第26000次数损失为1.4102176994680586\n",
      "第27000次数损失为1.3780924919258466\n",
      "第28000次数损失为1.3472849060019343\n",
      "第29000次数损失为1.3317936086341575\n",
      "第30000次数损失为1.305540338191757\n",
      "第31000次数损失为1.2801070712932732\n",
      "第32000次数损失为1.2553380120367699\n",
      "第33000次数损失为1.2307953365723687\n",
      "第34000次数损失为1.2057504213460315\n",
      "第35000次数损失为1.180833490986508\n",
      "第36000次数损失为1.156177441209871\n",
      "第37000次数损失为1.1319064872455442\n",
      "第38000次数损失为1.108392096521857\n",
      "第39000次数损失为1.086045145934924\n",
      "第40000次数损失为1.065100969401865\n",
      "第41000次数损失为1.0454593721802\n",
      "第42000次数损失为1.0266728528718028\n",
      "第43000次数损失为1.0081864435904908\n",
      "第44000次数损失为0.9896282778431242\n",
      "第45000次数损失为0.9709065820153239\n",
      "第46000次数损失为0.9521192003690534\n",
      "第47000次数损失为0.9334027120201607\n",
      "第48000次数损失为0.9148694572954047\n",
      "第49000次数损失为0.8966824307586465\n",
      "第50000次数损失为0.87906916145202\n",
      "第51000次数损失为0.8621963118056682\n",
      "第52000次数损失为0.8460973330560747\n",
      "第53000次数损失为0.8307121643913228\n",
      "第54000次数损失为0.8159720209478062\n",
      "第55000次数损失为0.8018793534228253\n",
      "第56000次数损失为0.7885234406250665\n",
      "第57000次数损失为0.7760203324385477\n",
      "第58000次数损失为0.7644380651701002\n",
      "第59000次数损失为0.7537636346837797\n",
      "第60000次数损失为0.7439161115364312\n",
      "第61000次数损失为0.7347812553701856\n",
      "第62000次数损失为0.7262473497352729\n",
      "第63000次数损失为0.7182290190346332\n",
      "第64000次数损失为0.71066726004178\n",
      "第65000次数损失为0.7035126919653958\n",
      "第66000次数损失为0.6987392794918429\n",
      "第67000次数损失为0.6905237297573199\n",
      "第68000次数损失为0.688619296562338\n",
      "第69000次数损失为0.6788255314009343\n",
      "第70000次数损失为0.6726655233273587\n",
      "第71000次数损失为0.6670775903507097\n",
      "第72000次数损失为0.6627596364484365\n",
      "第73000次数损失为0.6530674322373585\n",
      "第74000次数损失为0.6488379133546449\n",
      "第75000次数损失为0.6453100833808022\n",
      "第76000次数损失为0.6406412718532285\n",
      "第77000次数损失为0.635266319564749\n",
      "第78000次数损失为0.6298112912931739\n",
      "第79000次数损失为0.6250110915256597\n",
      "第80000次数损失为0.6229523580923289\n",
      "第81000次数损失为0.6288093225384387\n",
      "第82000次数损失为0.6143693909978414\n",
      "第83000次数损失为0.6093743313466565\n",
      "第84000次数损失为0.6083543184107504\n",
      "第85000次数损失为0.6028704462248917\n",
      "第86000次数损失为0.5991833681620767\n",
      "第87000次数损失为0.5950083411368257\n",
      "第88000次数损失为0.5909639413519578\n",
      "第89000次数损失为0.5869854387499372\n",
      "第90000次数损失为0.5830720163142293\n",
      "第91000次数损失为0.5792243302310245\n",
      "第92000次数损失为0.5754451137922091\n",
      "第93000次数损失为0.5717377715253579\n",
      "第94000次数损失为0.5681051208470127\n",
      "第95000次数损失为0.5645490654194684\n",
      "第96000次数损失为0.5610706080149179\n",
      "第97000次数损失为0.5576698456132658\n",
      "第98000次数损失为0.5543460956787646\n",
      "第99000次数损失为0.5510983997108901\n",
      "第100000次数损失为0.5479264159072063\n",
      "第101000次数损失为0.5448313257569333\n",
      "第102000次数损失为0.5418161699304569\n",
      "第103000次数损失为0.5388852836939683\n",
      "第104000次数损失为0.5360430868083699\n",
      "第105000次数损失为0.5332929002405609\n",
      "第106000次数损失为0.530636344727875\n",
      "第107000次数损失为0.5280733197275173\n",
      "第108000次数损失为0.5256020036748462\n",
      "第109000次数损失为0.5232183298325954\n",
      "第110000次数损失为0.5209151936906007\n",
      "第111000次数损失为0.5186823844530014\n",
      "第112000次数损失为0.5165078543384538\n",
      "第113000次数损失为0.5143798978142194\n",
      "第114000次数损失为0.5122893286206305\n",
      "第115000次数损失为0.5102309144471622\n",
      "第116000次数损失为0.5082035468996665\n",
      "第117000次数损失为0.5062089725443106\n",
      "第118000次数损失为0.5042495932236808\n",
      "第119000次数损失为0.502326335539054\n",
      "第120000次数损失为0.5004373685075792\n",
      "第121000次数损失为0.4985778015568143\n",
      "第122000次数损失为0.49674004474073485\n",
      "第123000次数损失为0.49491445915407944\n",
      "第124000次数损失为0.493090070320509\n",
      "第125000次数损失为0.4912541231005827\n",
      "第126000次数损失为0.48798856223695164\n",
      "第127000次数损失为0.4874140511542272\n",
      "第128000次数损失为0.473458879622475\n",
      "第129000次数损失为0.48332142651195065\n",
      "第130000次数损失为0.4693936970835866\n",
      "第131000次数损失为0.4671226129501914\n",
      "第132000次数损失为0.5175355944721829\n",
      "第133000次数损失为0.4622373278479496\n",
      "第134000次数损失为0.4644340979161574\n",
      "第135000次数损失为0.47356355030397845\n",
      "第136000次数损失为0.5047537929666736\n",
      "第137000次数损失为0.4849949970357748\n",
      "第138000次数损失为0.4509501956698146\n"
     ]
    }
   ],
   "source": [
    "# 输入数据进行训练\n",
    "train = neuralnet.train(x_train, y_train, hiddenNum=3, unitNum=18, alpha=0.1, maxIters=1000000, precision=0.01)\n",
    "# 读取神经网络的参数\n",
    "Thetas = train['Thetas']\n",
    "# 获得预测值\n",
    "train_predictions = neuralnet.predict(x_train, Thetas)\n",
    "train_predictions = train_predictions.transpose()\n",
    "train_predict = train_predictions.argmax(axis=1)\n",
    "print(train_predict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_accuracy = precision_score(Y_train, train_predict, average='micro')*100\n",
    "print(\"训练集准确率：{} %\" .format(train_accuracy))\n",
    "train_matrix = confusion_matrix(Y_train, train_predict)\n",
    "print(train_matrix)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 获得预测值\n",
    "test_predictions = neuralnet.predict(x_test, Thetas)\n",
    "test_predictions = test_predictions.transpose()\n",
    "test_predict = test_predictions.argmax(axis=1)\n",
    "test_accuracy = precision_score(Y_test, test_predict, average='micro')*100\n",
    "print(\"训练集准确率：{} %\" .format(test_accuracy))\n",
    "test_matrix = confusion_matrix(Y_test, test_predict)\n",
    "print(test_matrix)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
